import { tryPermission } from '@/api/index'
import { rulesMenu } from '@/utils';
import { menu } from '@/router/menu'

export const dynamicmenu = {
    namespaced: true,
    state: {
        dymenu:[]
    },
    actions: {
        // 携带token请求后端对应的权限
        // 动态更新菜单
        async updateDyMenu({commit,rootState}){
            let res=await tryPermission({token:rootState.login.userInfo.token});
            if(res.status===200){
                let newMenu=rulesMenu(menu,res.data.data);
                console.log('dydy',res,'newMenu',newMenu);
                commit('setMenuList',newMenu);
                return newMenu;
            }
        }
    },
    mutations: {
        setMenuList(state,payload){
            state.dymenu=payload;
        },
        clearMenuList(state){
            state.dymenu=[];
        }
    }
}